package com.xzlcPT.service.impl;

import com.github.pagehelper.PageHelper;
import com.util.PageBean;
import com.xzlcPT.bean.*;
import com.xzlcPT.dao.*;
import com.xzlcPT.service.XzPostionSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2017/7/26.
 */
@Service("postionSendService")
public class XzPostionSendServiceImpl implements XzPostionSendService {

    @Autowired
    private XzPostionSendMapper postionSendMapper;
    @Autowired
    private XzPostionMapper postionMapper;
    @Autowired
    private XzCompanyMapper companyMapper;
    @Autowired
    private XzCompanyDomainMapper companyDomainMapper;
    @Autowired
    private XzPostionSendMsgMapper postionSendMsgMapper;
    @Autowired
    private XzResumeMapper resumeMapper;
    @Autowired
    private XzMemberMapper memberMapper;
    @Autowired
    private XzMsgMapper msgMapper;

    @Override
    public PageBean<XzPostionSend> selSendByRIdAndType(int page, int rows, Long resumeId, Integer type) {
        Map sMap = new HashMap();
        if (type != 8) {
            sMap.put("type", type);
        }
        sMap.put("resumeId", resumeId);
        PageHelper.startPage(page, rows);
        List<XzPostionSend> postionSends = postionSendMapper.selSendByRIdAndType(sMap);
        postionSends.forEach(ps -> {
            XzPostion postion = postionMapper.selectByPrimaryKey(ps.getPostionId());
            XzCompany company = companyMapper.selectByPrimaryKey(ps.getCompanyId());
            XzPostionSendMsg postionSendMsg = postionSendMsgMapper.selectBySendId(ps.getSendId());
            List<XzCompanyDomain> companyDomains = companyDomainMapper.selectByCompanyId(company.getCompanyId());
            company.setDomains(companyDomains);
            ps.setPostion(postion);
            ps.setCompany(company);
            ps.setPostionSendMsg(postionSendMsg);
        });
        return new PageBean<>(postionSends);
    }

    @Override
    public List<XzPostionSend> selSendCountByCorRId(Map map) {
        return postionSendMapper.selSendCountByCorRId(map);
    }

    @Override
    public Integer selisSend(Map map) {
        return postionSendMapper.selisSend(map);
    }

    @Override
    public int insertSelective(XzPostionSend xzPostionSend) {
        int i = postionSendMapper.insertSelective(xzPostionSend);
        return i;
    }

    @Override
    public int selSendCount(Long resumeId) {
        int i = postionSendMapper.selSendCount(resumeId);
        return i;
    }

    @Override
    public PageBean<XzPostionSend> selByState(int page, int rows, Map map) {
        PageHelper.startPage(page, rows);
        List<XzPostionSend> list2 = postionSendMapper.selByState(map);
        PageBean pageBean = new PageBean(list2);
        List<XzPostionSend> list1 = pageBean.getList();
        List<Long> list = new ArrayList<>();
        for (XzPostionSend xzPostionSend : list1) {
            list.add(xzPostionSend.getSendId());
        }
        Map map1 = new HashMap();
        map1.put("list", list);
        if (map.get("zw") != null) {
            map1.put("zw", map.get("zw").toString());
        }
        List<XzPostionSend> postionSendList = postionSendMapper.selBysendId(map1);
        pageBean.setList(postionSendList);
        return pageBean;
    }

    @Override
    public PageBean<XzPostionSend> selByType(int page, int rows, Map map) {
        PageHelper.startPage(page, rows);
        List<XzPostionSend> list2 = postionSendMapper.selByType(map);
        PageBean pageBean = new PageBean(list2);
        List<XzPostionSend> list1 = pageBean.getList();
        List<Long> list = new ArrayList<>();
        for (XzPostionSend xzPostionSend : list1) {
            list.add(xzPostionSend.getSendId());
        }
        Map map1 = new HashMap();
        map1.put("list", list);
        if (map.get("zw") != null) {
            map1.put("zw", map.get("zw").toString());
        }
        List<XzPostionSend> postionSendList = postionSendMapper.selBysendId(map1);
        pageBean.setList(postionSendList);
        return pageBean;
    }

    @Override
    public PageBean<XzResume> selComCollect(int page, int rows, Map map) {
        PageHelper.startPage(page, rows);
        List<XzResume> list = postionSendMapper.selComCollect(map);
        PageBean pageBean = new PageBean(list);
        return pageBean;
    }

    //20171213-王继顺
    @Override
    public PageBean<XzPostionSend> selSendByPostionId(int page, int rows, Long postionId) {
        PageHelper.startPage(page, rows);
        List<XzPostionSend> list = postionSendMapper.selSendByPostionId(postionId);
        return new PageBean<>(list);
    }

    @Override
    public int deleteByPrimaryKey(Long sendId) {
        int i = postionSendMapper.deleteByPrimaryKey(sendId);
        return i;
    }

    @Override
    public int updateState(Map map, XzLogin xzLogin, Long sendId) {
        int i = postionSendMapper.updateState(map);
        XzPostionSend xzPostionSend = postionSendMapper.selectByPrimaryKey(sendId);
        XzResume xzResume = resumeMapper.selectByPrimaryKey(xzPostionSend.getResumeId());
        Map map2 = new HashMap();
        map2.put("companyId", xzLogin.getCompany().getCompanyId());
        map2.put("memberId", xzResume.getMemberId());
        int s = msgMapper.selSendMsg(map2);
        if (s == 0) {
            Date t1 = new Date();
            XzMsg xzMsg = new XzMsg();
            xzMsg.setMsgTital("有公司邀请您面试");
            xzMsg.setMsgContent(xzLogin.getCompany().getCompanyName() + "邀请您参加面试");
            xzMsg.setMsgSendId(xzLogin.getLoginId());
            xzMsg.setMsgSendName(xzLogin.getCompany().getCompanyName());
            xzMsg.setMsgReceiveId(memberMapper.selLoginId(xzResume.getMemberId()));
            xzMsg.setMsgReceiveName(xzResume.getResumeName());
            xzMsg.setMsgTime(t1);
            xzMsg.setMsgType(1);
            xzMsg.setMsgIsread(0);
            xzMsg.setOutId(xzLogin.getLoginId());
            xzMsg.setCreateTime(t1);
            msgMapper.insertSelective(xzMsg);
        }
        return i;
    }

    @Override
    public int updateState1(Long sendId) {
        int i = postionSendMapper.updateState1(sendId);
        return i;
    }

    @Override
    public Long selByCollect(XzPostionSend xzPostionSend) {
        Long sendId = postionSendMapper.selByCollect(xzPostionSend);
        return sendId;
    }

    @Override
    public PageBean<XzPostionSend> selByConditions(int page, int rows, Map map) {
        PageHelper.startPage(page, rows);
        List<XzPostionSend> list2 = postionSendMapper.selByConditions(map);
        PageBean pageBean = new PageBean(list2);
        List<XzPostionSend> list1 = pageBean.getList();
        List<Long> list = new ArrayList<>();
        for (XzPostionSend xzPostionSend : list1) {
            list.add(xzPostionSend.getSendId());
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        Map map1 = new HashMap();
        map1.put("list", list);
        if (map.get("zw") != null) {
            map1.put("zw", map.get("zw").toString());
        }
        List<XzPostionSend> postionSendList = postionSendMapper.selBysendId(map1);
        pageBean.setList(postionSendList);
        return pageBean;
    }

    @Override
    public int delAllChoose(List<Long> list) {
        int i = postionSendMapper.delAllChoose(list);
        return i;
    }

    @Override
    public XzPostionSend selBykey(Long sendId) {
        XzPostionSend xzPostionSend = postionSendMapper.selectByPrimaryKey(sendId);
        return xzPostionSend;
    }

    @Override
    public int updateState2(Map map, XzLogin xzLogin, Long sendId, String msg) {
        int i = postionSendMapper.updateState2(map);
        XzPostionSend xzPostionSend = postionSendMapper.selectByPrimaryKey(sendId);
        XzResume xzResume = resumeMapper.selectByPrimaryKey(xzPostionSend.getResumeId());
        Date t1 = new Date();
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital("您与公司的需求不匹配");
        xzMsg.setMsgContent(msg);
        xzMsg.setMsgSendId(xzLogin.getLoginId());
        xzMsg.setMsgSendName(xzLogin.getCompany().getCompanyName());
        xzMsg.setMsgReceiveId(memberMapper.selLoginId(xzResume.getMemberId()));
        xzMsg.setMsgReceiveName(xzResume.getResumeName());
        xzMsg.setMsgTime(t1);
        xzMsg.setMsgType(4);
        xzMsg.setMsgIsread(0);
        xzMsg.setOutId(xzLogin.getLoginId());
        xzMsg.setCreateTime(t1);
        msgMapper.insertSelective(xzMsg);
        return i;
    }

    @Override
    public int insertSelective2(XzPostionSend xzPostionSend, XzLogin userLogin) {
        int i = postionSendMapper.insertSelective(xzPostionSend);
        XzCompany xzCompany = companyMapper.selectByPrimaryKey(xzPostionSend.getCompanyId());
        Date t1 = new Date();
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital("有用户投递了简历");
        xzMsg.setMsgContent(userLogin.getMember().getMemberName() + "投递了简历");
        xzMsg.setMsgSendId(userLogin.getMember().getMemberId());
        xzMsg.setMsgSendName(userLogin.getMember().getMemberName());
        xzMsg.setMsgReceiveId(xzPostionSend.getCompanyId());
        xzMsg.setMsgReceiveName(xzCompany.getCompanyName());
        xzMsg.setMsgTime(t1);
        xzMsg.setMsgType(3);
        xzMsg.setMsgIsread(0);
        xzMsg.setOutId(xzCompany.getCompanyId());
        xzMsg.setCreateTime(t1);
        msgMapper.insertSelective(xzMsg);
        return i;
    }

    @Override
    public Date selPosSendtime(Long resumeId, Long postionId) {
        return postionSendMapper.selPosSendtime(resumeId, postionId);
    }

}
